#include "EventLoopThread.h"

EventLoopThread::EventLoopThread() :
    cond_(mutex_),
    thread_(std::bind(&EventLoopThread::threadFunction, this))
{

}

EventLoop* EventLoopThread::startLoop() {
    thread_.start();
    while (loop_ == NULL) {
        MutexGuard lock(mutex_);
        cond_.wait();
    }
    return loop_;
}

void EventLoopThread::threadFunction() {
    EventLoop loop;
    loop_ = &loop;
    {
        MutexGuard lock(mutex_);
        cond_.notify();
    }
    loop_->loop();
}